def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def rotations(t):
for i in range(1, len(t)+1):
if t == t[i:] + t[:i]:
return i
for _ in range(int(input())):
n = int(input())
s = input()
p = [int(i)-1 for i in input().split()]
visited = [False]*n
ans = 1
for i in range(n):
if visited[i]:
continue
visited[i] = True
j = p[i]
t = s[i]
while j != i:
t += s[j]
visited[j] = True
j = p[j]
rotation = rotations(t)
ans = ans*rotation//gcd(ans, rotation)
print(ans)
// Problem: F. Shifting String
// Contest: Codeforces - Codeforces Round #797 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1690/F
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
#define pb push_back
#define endl '\n'
#define mod 1000000007
#define N 500005
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define control ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int long long int
#define ffor(i, n) for(int i=0; i<n; i++)
#define fforr(i, n) for(int i=1; i<=n; i++)
#define rfor(i, n) for(int i=n-1; i>=0; i--)
#define rforr(i, n) for(int i=n; i>0; i--)
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define inf 9223372036854775807
using namespace std;
void setIO()
{
control;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void print(bool chk){
if(chk){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
}
int min(int a, int b){
return a>b?b:a;
}
int max(int a, int b){
return a<b?b:a;
}
int power(int x,int y,int p)
{
int ans=1;
x=x%p;
if(x==0)
{
return 0;
}
while(y>0)
{
if(y&1)
{
ans=(ans*x)%p;
}
y=y>>1;
x=(x*x)%p;
}
return ans;
}
int32_t main(){
int t; cin >> t;
while(t--){
int n; cin >> n;
string s; cin >> s;
vi v(n);
ffor(i, n){
cin >> v[i];
v[i]--;
}
vi vis(n);
vi res;
ffor(i, n){
if(!vis[i]){
vi temp;
temp.pb(i);
vis[i] = 1;
int x = v[i];
while(x != i){
vis[x] = 1;
temp.pb(x);
x = v[x];
}
int d = temp.size();
bool test = 0;
int mn = INT_MAX;
for(int j=1; j*j<=d; j++){
if(d%j == 0){
int l = j, r = d/j;
bool flag = 1;
for(int k=0; k<l; k++){
for(int p=k+l; p<d; p+=l){
if(s[temp[p]] != s[temp[k]]){
flag = 0;
break;
}
}
if(!flag)break;
}
if(flag){
mn = min(mn, l);
}
flag = 1;
for(int k=0; k<r; k++){
for(int p=k+r; p<d; p+=r){
if(s[temp[p]] != s[temp[k]]){
flag = 0;
break;
}
}
if(!flag)break;
}
if(flag){
mn = min(mn, r);
}
}
}
res.pb(mn);
}
}
int ans = 1;
for(int x:res){
ans = ans*x/__gcd(ans, x);
}
cout << ans << endl;
}
}
1359A - Berland Poker | 459A - Pashmak and Garden |
1327B - Princesses and Princes | 1450F - The Struggling Contestant |
1399B - Gifts Fixing | 1138A - Sushi for Two |
982C - Cut 'em all | 931A - Friends Meeting |
1594A - Consecutive Sum Riddle | 1466A - Bovine Dilemma |
454A - Little Pony and Crystal Mine | 2A - Winner |
1622B - Berland Music | 1139B - Chocolates |
1371A - Magical Sticks | 1253A - Single Push |
706B - Interesting drink | 1265A - Beautiful String |
214A - System of Equations | 287A - IQ Test |
1108A - Two distinct points | 1064A - Make a triangle |
1245C - Constanze's Machine | 1005A - Tanya and Stairways |
1663F - In Every Generation | 1108B - Divisors of Two Integers |
1175A - From Hero to Zero | 1141A - Game 23 |
1401B - Ternary Sequence | 598A - Tricky Sum |